const playerConfig = {
  jumpCountMax: 2,// 最大连跳次数
  downSpeed: 0.5,// 下落加速度增幅
  vy: -7,// 下落加速度
  flyvy: 3// 上升速度
}

export default class Player extends Sprite {

  constructor(flyEnergy, speedEnergy) {
    super();

    this.action = '';// 当前动作

    this.body = null;// 玩家

    this.jumpCount = 0;// 跳 统计数
    this.jumpCountMax = playerConfig.jumpCountMax;

    this.downSpeed = playerConfig.downSpeed;
    this.vy = 0;// 下落速度

    this.width = Config.playerWH;// 玩家的宽、高
    this.height = Config.playerWH;

    this.speedEnergy = speedEnergy;
    this.flyEnergy = flyEnergy;

    this.originalSpeed = 0;

    this.init();
  }

  init() {

    if(!Player.cached) {// 把动画缓存起来
      Player.cached = true;
      Animation.createFrames(['res/player/chara_01.png', 'res/player/chara_02.png', 'res/player/chara_03.png', 'res/player/chara_04.png'], Player.RUN);
      Animation.createFrames(['res/player/chara_05.png', 'res/player/chara_06.png', 'res/player/chara_07.png', 'res/player/chara_08.png'], Player.FLY);
      // Animation.createFrames(['res/player/chara_09.png','res/player/chara_10.png','res/player/chara_11.png','res/player/chara_12.png'], Player.HERT);
      Animation.createFrames(['res/player/chara_13.png', 'res/player/chara_14.png', 'res/player/chara_15.png', 'res/player/chara_16.png'], Player.JUMP);
    }

    const playerY = Config.playerWH - Config.grassHeight;

    this.body = new Animation();
    this.body.pivot(0, playerY);// xy position位置
    this.body.interval = 100;
    this.addChild(this.body);

    // 闪光效果
    const texture = Laya.loader.getRes("res/spiritEffect.png");
    this.spiritEffect = new Sprite();
    this.spiritEffect.graphics.drawTexture(texture, 0, 0, 153, 191);
    this.spiritEffect.pivot(153 / 2, 191 / 2);
    this.spiritEffect.x = 48;
    this.spiritEffect.visible = false;
    this.spiritEffect.scale(5, 5);
    this.addChild(this.spiritEffect);

    // 残影效果1
    this.bodyEffect1 = new Animation();
    this.bodyEffect1.alpha = 0.6;
    this.bodyEffect1.pivot(25, playerY);
    this.bodyEffect1.interval = 100;
    this.bodyEffect1.visible = false;
    this.addChild(this.bodyEffect1);
    // 残影效果2
    this.bodyEffect2 = new Animation();
    this.bodyEffect2.alpha = 0.3;
    this.bodyEffect2.pivot(50, playerY);
    this.bodyEffect2.interval = 100;
    this.bodyEffect2.visible = false;
    this.addChild(this.bodyEffect2);

    // 播放动作对应的动画
    this.playAction(Player.RUN);
    // 创建一个帧循环处理函数
    Laya.timer.frameLoop(1, this, this.onLoop);
  }

  playAction(action = '') {

    if(this.action === action) return ;

    this.action = action;
    this.body.play(0, true, action);
    this.bodyEffect1.play(0, true, action);
    this.bodyEffect2.play(0, true, action);
  }

  onLoop() {

    if(gamePauseHandle(this, this.onLoop)) return ;

    if(this.isEffect()) {// 人物处于加速特效
      this.speedEnergy.changeValue(-0.3);
      if(this.speedEnergy.value <= 1){
        this.hideEffect();
        this.gotoRun();
      }
    }

    this.y += Math.min(this.vy, 12);// 15是最大下落速度
    this.vy += this.downSpeed;

    if(this.y > (Config.deviceHeight + 100)) {// 游戏结束
      console.log('Game over');
      this.event(Player.DIE, this);
      return ;
    }

    switch(this.action){
      case Player.FLY: {//如果当前是飞行状态 将玩家Y轴慢慢往上提 并且不超过最大值

        if(!this.isEffect()) this.flyEnergy.changeValue(-0.3);

        if(this.flyEnergy.value <= 1) {
          this.gotoJump();
        } else {
          this.vy = 0;
          this.y -= playerConfig.flyvy;
          this.y = Math.max(Config.currentFloory, this.y);
        }
        break;
      }
      default: this.flyEnergy.changeValue(0.05);
    }
  }

  gotoJump() {
    this.playAction(Player.JUMP);
  }

  gotoFly() {
    this.playAction(Player.FLY);
  }

  gotoRun() {
    this.playAction(Player.RUN);
  }

  jump() {

    if(this.jumpCount < this.jumpCountMax) {
      this.vy = playerConfig.vy;
      this.jumpCount++;
      this.gotoJump();
    } else {
      this.gotoFly();
    }
  }

  jumpReset() {
    this.vy = 0;
    this.jumpCount = 0;
    this.gotoRun();
  }

  // 是否处于加速特效效果中
  isEffect() {
    return this.bodyEffect1.visible;
  }

  // 开始显示特效
  showEffect() {

    this.speedEnergy.changeValue(100);
    Config.isPause = true;
    !this.isEffect() && (this.originalSpeed = Config.baseSpeed);
    Config.baseSpeed = this.originalSpeed * 2.5;
    this.spiritEffect.visible = true;
    Tween.to(this.spiritEffect, { scaleX : 0.1, scaleY : 0.1, rotation : 360 }, 1000, null, Handler.create(this, this.spiritEffectTweenComplete));
  }

  hideEffect() {

    this.bodyEffect1.visible = false;
    this.bodyEffect2.visible = false;
    Config.baseSpeed = this.originalSpeed;
  }

  spiritEffectTweenComplete() {

    this.spiritEffect.visible = false;
    this.spiritEffect.scale(5, 5);
    this.spiritEffect.rotation = 0;
    this.bodyEffect1.visible = true;
    this.bodyEffect2.visible = true;
    Config.isPause = false;
  }
}

// 跑
Player.RUN = "player_run";
// 飞
Player.FLY = "player_fly";
// 暂时没有用到的动作
Player.HERT = "player_hert";
// 跳
Player.JUMP = "player_jump";
// 状态
Player.DIE = "player_die";
// 是否缓存了
Player.cached = false;
Player.show = 'player_show';